package com.alex.core.logUtils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author cb
 * @date 2019/10/18 14:24
 */
@Component
@Slf4j
public class CommonLogUtil {

    public static final String COMMON_LOG_QUEUE = "common-log-queue";
    public static final String COMMON_LOG_EXCHANGE = "common-log";
    public static final String COMMON_LOG_KEY = "log";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 记录日志
     * @param userId
     * @param username 账号
     * @param content 记录内容
     * @param source 原始值
     * @param type 日志类型，增删改查
     *             @see CommonLogType@RefreshScope
     * @param module 模块
     * @param uri uri
     * @param ip ip地址
     */
    @Deprecated
    public void recordCommonLog(Integer userId, String username, String content, String source, Integer type, String module, String uri, String ip) {
        CommonLog commonLog = new CommonLog();
        commonLog.setUserid(userId);
        commonLog.setUsername(username);
        commonLog.setSource(source);
        commonLog.setLogTime(new Date());
        commonLog.setContent(content);
        commonLog.setType(type);
        commonLog.setModule(module);
        commonLog.setUrl(uri);
        commonLog.setIp(ip);
        try {
            rabbitTemplate.convertAndSend(COMMON_LOG_EXCHANGE, COMMON_LOG_KEY, JSONObject.toJSONString(commonLog));
        } catch (Exception e) {
            log.error("发送日志到消息队列出错，日志会记录到相应的项目日志文件中", e);
            log.info("记录日志：{}", JSONObject.toJSONString(commonLog));
        }
    }

    /**
     * 记录日志
     * @param userId
     * @param username 账号
     * @param content 记录内容
     * @param source 原始值
     * @param type 日志类型，增删改查
     *             @see CommonLogType
     * @param module 模块
     * @param uri uri
     * @param ip ip地址
     * @param operatorType 操作人类型
     *              @see com.ddcx.contants.enums.AccountTypeEnum
     */
    @Deprecated
    public void recordCommonLog(Integer userId, String username, String content, String source, Integer type, String module, String uri, String ip, Integer operatorType) {
        CommonLog commonLog = new CommonLog();
        commonLog.setUserid(userId);
        commonLog.setUsername(username);
        commonLog.setSource(source);
        commonLog.setLogTime(new Date());
        commonLog.setContent(content);
        commonLog.setType(type);
        commonLog.setModule(module);
        commonLog.setUrl(uri);
        commonLog.setIp(ip);
        commonLog.setOperatorType(operatorType);
        try {
            rabbitTemplate.convertAndSend(COMMON_LOG_EXCHANGE, COMMON_LOG_KEY, JSONObject.toJSONString(commonLog));
        } catch (Exception e) {
            log.error("发送日志到消息队列出错，日志会记录到相应的项目日志文件中", e);
            log.info("记录日志：{}", JSONObject.toJSONString(commonLog));
        }
    }

    /**
     * 记录日志
     * @param userId
     * @param username 账号
     * @param target 修改后的值
     * @param source 原始值
     * @param type 日志类型，增删改查
     *             @see CommonLogType
     * @param module 模块
     * @param uri uri
     * @param ip ip地址
     * @param operatorType 操作人类型
     *              @see com.ddcx.contants.enums.AccountTypeEnum
     * @param message 记录日志内容
     */
    public void recordCommonLog(Integer userId, String username, String target, String source, Integer type, String module, String uri, String ip, Integer operatorType, String message) {
        CommonLog commonLog = new CommonLog();
        commonLog.setUserid(userId);
        commonLog.setUsername(username);
        commonLog.setSource(source);
        commonLog.setLogTime(new Date());
        commonLog.setContent(target);
        commonLog.setType(type);
        commonLog.setModule(module);
        commonLog.setUrl(uri);
        commonLog.setIp(ip);
        commonLog.setOperatorType(operatorType);
        commonLog.setMessage(message);
        try {
            rabbitTemplate.convertAndSend(COMMON_LOG_EXCHANGE, COMMON_LOG_KEY, JSONObject.toJSONString(commonLog));
        } catch (Exception e) {
            log.error("发送日志到消息队列出错，日志会记录到相应的项目日志文件中", e);
            log.info("记录日志：{}", JSONObject.toJSONString(commonLog));
        }
    }

}
